package com.viewpager2.teach;

import android.graphics.drawable.Drawable;
import android.os.Bundle;
import android.widget.LinearLayout;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.appcompat.app.AppCompatActivity;
import androidx.core.content.ContextCompat;
import androidx.viewpager2.widget.MarginPageTransformer;
import androidx.viewpager2.widget.ViewPager2;

import com.google.android.material.tabs.TabLayout;
import com.google.android.material.tabs.TabLayoutMediator;
import com.viewpager2.teach.adapter.ViewPager2ThreeAdapter;

import java.util.ArrayList;
import java.util.List;

/**
 * Creator:Yx
 * Time:2020/8/5 10:18
 * Description:ViewPager+TabLayout+Fragment
 */
public class TextThreeActivity extends AppCompatActivity {
    private TabLayout tabLayout;
    private ViewPager2 viewpager2;
    private ViewPager2ThreeAdapter adapter;

    private List<String> tabTitle = new ArrayList<>();

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_text_three);
        //导航
        initTypeListener();
        //ViewPager2
        initViewPager2();
        //导航数据
        tabTitle.add("栏目S");
        tabTitle.add("栏目T");
        tabTitle.add("栏目B");
        tabTitle.add("栏目O");

        //将TabLayout 与ViewPager2关联
        TabLayoutMediator tabLayoutMediator = new TabLayoutMediator(tabLayout, viewpager2, new TabLayoutMediator.TabConfigurationStrategy() {
            @Override
            public void onConfigureTab(@NonNull TabLayout.Tab tab, int position) {
                tab.setText(tabTitle.get(position));
            }
        });
        tabLayoutMediator.attach();
        adapter.setFragmentCount(tabTitle);
    }

    /**
     * 导航类型
     */
    private void initTypeListener() {
        tabLayout = findViewById(R.id.public_tabLayout);
        tabLayout.setTabGravity(TabLayout.GRAVITY_FILL);//填满一个屏幕 可以左右滑动
        tabLayout.setTabMode(TabLayout.MODE_FIXED);//充满
        tabLayout.addOnTabSelectedListener(new TabLayout.OnTabSelectedListener() {
            @Override
            public void onTabSelected(TabLayout.Tab tab) {
                TextView title = (TextView) (((LinearLayout) ((LinearLayout) tabLayout.getChildAt(0)).getChildAt(tab.getPosition())).getChildAt(1));
                Drawable rightDrawable = ContextCompat.getDrawable(TextThreeActivity.this, R.mipmap.ic_launcher);
                rightDrawable.setBounds(0, 0, rightDrawable.getMinimumWidth(), rightDrawable.getMinimumHeight());
                //void android.widget.TextView.setCompoundDrawables(Drawable left, Drawable top, Drawable right, Drawable bottom)
                title.setCompoundDrawables(null, null, rightDrawable, null);
                //title.setTextSize(20);
                //title.setTypeface(Typeface.defaultFromStyle(Typeface.BOLD));
                //selectTab(tab.getPosition());
            }

            @Override
            public void onTabUnselected(TabLayout.Tab tab) {
                //TextView title = (TextView) (((LinearLayout) ((LinearLayout) tabLayout.getChildAt(0)).getChildAt(tab.getPosition())).getChildAt(1));
                //title.setTextSize(16);
                //title.setTypeface(Typeface.defaultFromStyle(Typeface.NORMAL));
            }

            @Override
            public void onTabReselected(TabLayout.Tab tab) {

            }
        });
    }

    private void initViewPager2() {
        viewpager2 = findViewById(R.id.public_viewPager2);
        //默认是水平方向ORIENTATION_HORIZONTAL,垂直是ORIENTATION_VERTICAL
        viewpager2.setOrientation(ViewPager2.ORIENTATION_HORIZONTAL);
        //设置页与页的间距
        viewpager2.setPageTransformer(new MarginPageTransformer(15));
        //设置翻页动画
        //CompositePageTransformer transformer = new CompositePageTransformer();
        //设置Adapter
        adapter = new ViewPager2ThreeAdapter(this);
        viewpager2.setAdapter(adapter);
        //设置预加载页数
        viewpager2.setOffscreenPageLimit(1);//在动态加载数据时ViewPager中的Fragment实际都被懒加载了，-1等等根本没啥用
        //设置是否允许用户输入/触摸。false用户就禁止手滑不了页。
        viewpager2.setUserInputEnabled(true);
        //设置翻页监听
        viewpager2.registerOnPageChangeCallback(new ViewPager2.OnPageChangeCallback() {
            @Override
            public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {
                super.onPageScrolled(position, positionOffset, positionOffsetPixels);
            }

            @Override
            public void onPageSelected(int position) {
                super.onPageSelected(position);
            }

            @Override
            public void onPageScrollStateChanged(int state) {
                super.onPageScrollStateChanged(state);
            }
        });

    }


}
